# Contributor: Damian Kurek <starfire24680@gmail.com>
# Maintainer: Leon Marz <main@lmarz.org>
pkgname=osl
pkgver=1.13.11.0
pkgrel=0
pkgdesc="Advanced shading language for production GI renderers"
url="https://github.com/AcademySoftwareFoundation/OpenShadingLanguage"
# armhf armv7 x86: only supports 64-bit
# s390x: blocked by openimageio
arch="all !armhf !armv7 !s390x !x86"
license="BSD-3-Clause"
_llvmver=18
makedepends="cmake
	bison
	boost-dev
	clang$_llvmver-dev
	flex-dev
	fmt-dev
	lld
	llvm$_llvmver-dev
	llvm$_llvmver-libs
	openexr-dev
	openimageio-dev
	openimageio-tools
	pugixml-dev
	python3-dev
	py3-pybind11-dev
	qt6-qtbase-dev
	samurai
	zlib-dev
	"
checkdepends="
	font-droid
	py3-openimageio
	"
subpackages="
	$pkgname-libs
	$pkgname-dev
	$pkgname-doc
	py3-$pkgname:python
	openimageio-plugin-osl:_osl
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/AcademySoftwareFoundation/OpenShadingLanguage/archive/v$pkgver.tar.gz
	fix-install.patch
	"
builddir="$srcdir/OpenShadingLanguage-$pkgver"

# aarch64|loongarch64: incomplete required cpu features for target are not specified
# riscv64: Hard-float 'd' ABI can't be used for a target that doesn't support the D instruction set extension (ignoring target-abi)
case $CARCH in
	aarch64|loongarch64|riscv64) options="$options !check" ;;
esac

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi

	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_STANDARD=17 \
		-DCMAKE_SKIP_RPATH=ON \
		-DLLVM_ROOT="/usr/lib/llvm$_llvmver" \
		-DLLVM_BC_GENERATOR="/usr/bin/clang++-$_llvmver" \
		-DLLVM_AS_TOOL="/usr/lib/llvm$_llvmver/bin/llvm-as" \
		-DLLVM_LINK_TOOL="/usr/lib/llvm$_llvmver/bin/llvm-link" \
		-DLLVM_STATIC=OFF \
		-DOSL_BUILD_TESTS="$(want_check && echo ON || echo OFF)" \
		-DUSE_PARTIO=OFF \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	local exclude=
	case $CARCH in
		ppc64le) exclude="|render*|blackbody*" ;;
	esac
	cd build
	OIIO_LIBRARY_PATH=$builddir/build/lib \
	PYTHONPATH=$builddir/build/lib/python/site-packages \
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest -E "example-deformer$exclude"
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

python() {
	pkgdesc="Python3 bindings for OSL"
	depends="py3-openimageio"
	provides="$pkgname-python=$pkgver-r$pkgrel"
	amove usr/lib/python3*
}

doc() {
	default_doc
	# Example shaders
	amove usr/share/OSL/shaders/*.osl
	amove usr/share/OSL/shaders/*.oso
}

_osl() {
	pkgdesc="Plugin to access OSL from OpenImageIO"
	depends=""
	amove usr/lib/osl.imageio.so
}

sha512sums="
1ab1f3fd7b0a4e40afd2775ccc74229ca257e9c32e9d4ddf054daa456c17a0e65842cde6f8ffa469bc0fb2dc71f60a993545d446e9615b5ca5e90f7537785571  osl-1.13.11.0.tar.gz
6e640dc2b6814968306bc11aed89055dd87e1668544128c3665afbc6f5c2e0c3b7a30c24e85467ec1679da815dadb878a6825a9e01dd50af2570b3e548115bfb  fix-install.patch
"
